package com.allwees.bs.c.common.auth.model;

import com.allwees.bs.c.common.auth.constant.AuthConstant;
import com.allwees.bs.c.common.auth.constant.EAuthType;
import com.allwees.bs.c.module.user.entity.UserEntity;
import com.allwees.bs.core.model.user.constant.EUserSourceType;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.io.Serializable;


@Data
@NoArgsConstructor
@AllArgsConstructor
public class OAuthUserInfo implements Serializable {
	private static final long serialVersionUID = 2984272954673950383L;

	private EUserSourceType sourceType;

	private EAuthType authType;

	private String inviterShareCode;

	private String id;

	private String email;

	private String firstName;

	private String lastName;

	private String avatarUrl;

	public UserEntity toUserEntity(PasswordEncoder passwordEncoder) {
		UserEntity user = new UserEntity();
		switch(getAuthType()) {
			case GOOGLE:
			case MOCK_GOOGLE:
				user.setGoogleId(getId());
				break;
			case FACEBOOK:
				user.setFacebookId(getId());
				break;
			case APPLE:
				user.setAppleId(getId());
				break;
			default:
				break;
		}
		user.setFirstName(firstName);
		user.setLastName(lastName);
		user.setEmail(email);
		user.setPassword(passwordEncoder.encode(AuthConstant.DEFAULT_OAUTH_PASSWORD));
		user.setSourceType(sourceType);
		user.setAvatar(avatarUrl);
		user.setInviterShareCode(inviterShareCode);
		return user;
	}
}
